#!/usr/bin/env bash
# https://raw.githubusercontent.com/u-on/script/main/shell/module/github_releases.sh
# https://raw.fastgit.org/u-on/script/main/shell/module/github_releases.sh
# bash <(curl -sL https://raw.fastgit.org/u-on/script/main/shell/module/github_releases.sh) 'ventoy/ventoy' 'windows'
function GetReleasesFileUrl() {

    local repos name releasesUrl
    repos=$1
    name=$2
    releasesUrl="$(curl -s "https://api.github.com/repos/${repos}/releases/latest" | grep "\"browser_download_url\".*${name}" | cut -d '"' -f 4 | cut -d '/' -f 4-)"
    echo "$releasesUrl"

}

function Download() {
    local downloadUrl githubUrls
    downloadUrl=$1
    githubUrls=(
        https://hub.fastgit.org/
        https://github.com.cnpmjs.org/
        https://github.com/
    )

    for i in "${githubUrls[@]}"; do
        if curl -fLOk --retry 1 "$i$downloadUrl"; then
            break
        fi

    done
}

url=$(GetReleasesFileUrl "$@")
Download "$url"
